﻿using Abp.Dependency;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AH.ExportImport.Help;
using AH.Records;
using OfficeOpenXml;
using AH.Records.Dto;

namespace AH.WinFromUI
{
    public partial class MainFrom : Form, ITransientDependency
    {
        private readonly IProductAppService _productAppService;

        public MainFrom(IProductAppService productAppService)
        {
            _productAppService = productAppService;
            InitializeComponent();
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            //初始化一个OpenFileDialog类
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "(*.xlsx)|*.xlsx";

            //判断用户是否正确的选择了文件
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择文件的后缀名
                string extension = Path.GetExtension(fileDialog.FileName);
                //声明允许的后缀名
                string[] str = new string[] { ".xlsx" };
                if (!((IList)str).Contains(extension))
                {
                    MessageBox.Show("仅能上传xlsx格式的文档！");
                }
                else
                {
                    StreamReader read = File.OpenText(extension);
                  



                }
            }
        }

        public virtual void ImportProductsFromXlsx(Stream stream)
        {
            var properties = new[]
            {
                new PropertyByName<Product>("Asin"),
                new PropertyByName<Product>("Title"),
                new PropertyByName<Product>("type"),
                new PropertyByName<Product>("IsOrder")
            };

            var manager = new PropertyManager<Product>(properties);

            using (var xlPackage = new ExcelPackage(stream))
            {
                // get the first worksheet in the workbook
                var worksheet = xlPackage.Workbook.Worksheets.FirstOrDefault();
                if (worksheet == null)
                    throw new Exception("No worksheet found");

                var iRow = 2;

                while (true)
                {
                    var allColumnsAreEmpty = manager.GetProperties
                        .Select(property => worksheet.Cells[iRow, property.PropertyOrderPosition])
                        .All(cell => cell == null || cell.Value == null || String.IsNullOrEmpty(cell.Value.ToString()));

                    if (allColumnsAreEmpty)
                        break;

                    manager.ReadFromXlsx(worksheet, iRow);

                    var productInput = new AddNewProductInput
                    {
                        Asin = manager.GetProperty("Asin").StringValue,
                        Title = manager.GetProperty("Title").StringValue,
                        type = manager.GetProperty("type").StringValue,
                        IsOrder = manager.GetProperty("IsOrder").BooleanValue
                    };

                    _productAppService.AddNewProduct(productInput);

                    iRow++;
                }
            }
        }
    }
}
